// AWTMdiChildWnd.cpp : implementation file
//

#include "stdafx.h"
#include "AWTMdiChildWnd.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// AWTMdiChildWnd

IMPLEMENT_DYNCREATE(AWTMdiChildWnd, CMDIChildWnd)

AWTMdiChildWnd::AWTMdiChildWnd()
{
	m_applet = NULL;
}

AWTMdiChildWnd::~AWTMdiChildWnd()
{
}


BEGIN_MESSAGE_MAP(AWTMdiChildWnd, CMDIChildWnd)
	//{{AFX_MSG_MAP(AWTMdiChildWnd)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_SIZE()
	ON_WM_SETFOCUS()
	ON_MESSAGE(WM_AWTKEYEVENT, OnAWTKeyEvent)
	ON_MESSAGE(WM_AWTFOCUSEVENT, OnAWTFocusEvent)
	ON_MESSAGE(WM_AWTWINDOWEVENT, OnAWTWindowEvent)
	ON_WM_ERASEBKGND()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// AWTMdiChildWnd message handlers

BOOL AWTMdiChildWnd::PreCreateWindow(CREATESTRUCT& cs) 
{
	if (m_applet != NULL)
	{
		m_applet->setBackground(AWTColor::white());
		m_applet->setForeground(AWTColor::black());
	}
	return CMDIChildWnd::PreCreateWindow(cs);
}

int AWTMdiChildWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
//	SetWindowText(_T("!~AWTWindow~!"));

	if (m_applet != NULL)
	{
		CRect rc;
		GetClientRect(&rc);
		m_applet->setBounds(0,0,rc.Width(),rc.Height());
		
		m_applet->getFrame()->setPeer(this);
		m_applet->addNotify();	
		m_applet->init();
		m_applet->start();

		m_applet->validate();
	}
	
	return 0;
}

void AWTMdiChildWnd::OnDestroy() 
{
	CMDIChildWnd::OnDestroy();
	
	if (m_applet != NULL) 
	{
		m_applet->getFrame()->setPeer(NULL);
		m_applet->removeNotify();	
		m_applet->stop();
		m_applet->destroy();
	}
	delete m_applet;
}

void AWTMdiChildWnd::OnSize(UINT nType, int cx, int cy) 
{
	CMDIChildWnd::OnSize(nType, cx, cy);
	
	if (m_applet != NULL)
	{
		m_applet->setBounds(0,0,cx,cy);
		m_applet->invalidate();
		m_applet->validate();
	}
}

void AWTMdiChildWnd::OnSetFocus(CWnd* pOldWnd) 
{
	CMDIChildWnd::OnSetFocus(pOldWnd);
	
	if (m_applet != NULL)
	{
		m_applet->getFocusManager()->activateFocus();
	}
	return;
}

long AWTMdiChildWnd::OnAWTKeyEvent(WPARAM wp, LPARAM lp) 
{
	AWTKeyEvent* e = (AWTKeyEvent*)lp;
	switch (e->getId())
	{
		case AWTKeyEvent::KEY_PRESSED:
			if ((e->getKeyChar() == VK_TAB) && ((AWTComponent*)e->getSource())->isFocusTraversable())
			{
				AWTFocusManager* fm = m_applet->getFocusManager();
				if (::GetKeyState(VK_SHIFT) < 0)  //SHIFT is pressed
					fm->focusPrevious((AWTComponent*)e->getSource());
				else
					fm->focusNext((AWTComponent*)e->getSource());
			}
			break;
	}

	return 0;
}

long AWTMdiChildWnd::OnAWTFocusEvent(WPARAM wp, LPARAM lp) 
{
	AWTFocusEvent* e = (AWTFocusEvent*)lp;
	AWTFocusManager* fm;
	switch (e->getId())
	{
		case AWTFocusEvent::FOCUS_GAINED:
			fm = m_applet->getFocusManager();
			fm->setFocusOwner((AWTComponent*)e->getSource());
			break;
/*
		case AWTFocusEvent::FOCUS_LOST:
			fm = m_applet->getFocusManager();
			AWTComponent* c = (AWTComponent*)e->getSource();
			if (fm->getFocusOwner() == c)
				fm->setFocusOwner(NULL);
			break;
*/
	}

	return 0;
}

long AWTMdiChildWnd::OnAWTWindowEvent(WPARAM wp, LPARAM lp) 
{
	AWTWindowEvent* e = (AWTWindowEvent*)lp;
	AWTFocusManager* fm;
	switch (e->getId())
	{
		case AWTWindowEvent::WINDOW_CLOSED:
			fm = m_applet->getFocusManager();
			AWTComponent* c = (AWTComponent*)e->getSource();
			if (fm->getFocusOwner() == c)
				fm->setFocusOwner(NULL);
			break;
	}

	return 0;
}

BOOL AWTMdiChildWnd::OnEraseBkgnd(CDC* pDC) 
{
	return TRUE;
}
